<?php

namespace app\admin\controller;

use think\Request;

use data\service\ArticleService;
use data\service\LabelService;
use data\model\Article as articleModel;

class Article extends BaseController
{
    private $articleService;

    // 模型初始化
    public function initialize()
    {
        parent::initialize();
        $this->articleService = new ArticleService();
    }


    public function index(Request $request,$type='dailyLife'){
        $typeUs = articleModel::getArticleTypeOfUs();
        $typeId = array_search($type,$typeUs);

        $this->assign('labels',LabelService::getLabelByTypeId($typeId));

        $this->assign('typeId',$typeId);

        return $this->fetch();
    }

    /**
     * 添加文字
     */
    public function addArt(){
        return $this->fetch();
    }

    /**
     * 编辑文章
     * @date 2018-1-30
     */
    public function editArt($id){
        $article = $this->articleService->getById($id,['type_text','label_text']);

        $this->assign('data',$article);
        $this->assign('id',$id);

        return $this->fetch();
    }

    public function label(){
        /** 获取可添加的文章分类 */
        $types = $this->articleService->getArticleType();
        $this->assign('labelType',$types);

        return $this->fetch();
    }

    /**
     * 添加标签
     * @date 2018-1-24
     */
    public function addlabel(){
        /** 获取可添加的文章分类 */
        $types = $this->articleService->getArticleType();
        $this->assign('labelType',$types);

        return $this->fetch('addlabel');
    }

    /**
     * 编辑标签
     * @date 2018-1-26
     */
    public function editLabel($id){
        $types = $this->articleService->getArticleType();
        $this->assign('labelType',$types);

        $label = new LabelService();
        $data = $label->getById($id,['type_text']);
        $this->assign('data',$data);

        $this->assign('id',$id);
        return $this->fetch('editlabel');
    }

}
